package com.karry.service.impl;

import com.karry.mapper.RecordMapper;
import com.karry.pojo.Record;
import com.karry.service.RecordService;
import com.karry.util.Page;
import com.karry.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.ArrayList;

public class RecordServiceImpl implements RecordService {

    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public Page<Record> getAllRecordByPage(int pageNum) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        RecordMapper mapper = sqlSession.getMapper(RecordMapper.class);

        int count = mapper.getRecordCount();
        Page<Record> recordPage = new Page<>(pageNum, 13, count);
        ArrayList<Record> records = mapper.getRecordListByPage((pageNum - 1) * 13);
        recordPage.setDataList(records);
        sqlSession.close();
        return recordPage;

    }

    @Override
    public ArrayList<Record> searchAllRecord() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        RecordMapper mapper = sqlSession.getMapper(RecordMapper.class);

        ArrayList<Record> records = mapper.searchAllRecord();
        sqlSession.close();
        return records;
    }
}
